<?php

namespace App\Services;

use App\Models\AggregateReport;
use Illuminate\Database\Eloquent\Builder;

class AggregateReportService
{
    public static function isUsed(AggregateReport $aggregateReport)
    {
        switch ($aggregateReport->frequency) {
            case 'monthly':
                return AggregateReport::where([
                    'sheet_template_id' => $aggregateReport->sheet_template_id,
                    'frequency' => $aggregateReport->frequency,
                ])

                    ->where(function (Builder $query) use ($aggregateReport) {
                        $query->where([
                            ['year', '=', $aggregateReport->year],
                            ['key', '>', $aggregateReport->key],
                        ])
                            ->orWhere('year', '>', $aggregateReport->year);
                    })
                    ->exists();

            default:
                return false;
        }
    }
}
